package top.lvkun.user.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.FastDfsUtils;
import top.lvkun.user.domain.dto.UserDto;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            //获取上传文件名
            String filename = file.getOriginalFilename();
            //获取文件后缀
            String suffix = filename.substring(filename.lastIndexOf(".")+1);
            //上传文件并返回文件路径
            String filepath = FastDfsUtils.upload(file.getBytes(), suffix);
            System.out.println(filepath);
            return AjaxResult.me().setData(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.me().setSuccess(false).setMessage("上传失败！");
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody UserDto userDto){
        try {
//            String[] arr = filepath.split("/");
            FastDfsUtils.delete(userDto.getGroup(), userDto.getFilename());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().setSuccess(false).setMessage("删除失败！");
    }


}
